import 'package:flutter/material.dart';
import 'package:hxdj/common/base_page/my_loading_page.dart';
import 'package:hxdj/common/dio/dio_http.dart';
import 'package:hxdj/common/my_header_global.dart';

//选择支付页面
class SelectPayPage extends StatefulWidget {
  //回调
  final Map<String, dynamic>? object;

  const SelectPayPage({super.key, this.object});

  @override
  State<SelectPayPage> createState() => _SelectPayPageState();
}

class _SelectPayPageState extends State<SelectPayPage> {
  //选择的支付方式
  int selectIndex = 0;

  List<String> titles = [];

  List<String> imageNames = [];

  @override
  void initState() {
    super.initState();

    //请求支付的方式
    requestPayMode();
  }

  //请求支付的方式
  void requestPayMode() {
    // 去请求服务器数据.
    DioHttpManager().request(MyApi.getPayManager, method: DioHttpManager.post).then((resultMap) {
      Map<String, dynamic> resultPayManagerMap = resultMap["data"];
      titles = ["支付宝支付", "微信支付"];
      imageNames = ["ic_ali_pay", "ic_wechat_pay"];
      //遍历当前支持的支付方式
      resultPayManagerMap.forEach((key, value) {
        if (key == "is_open_xiaowei_fu") {
          //获取当前是否支持微信的支付方式
          if (value == "0") {
            titles.removeAt(1);
            imageNames.removeAt(1);
          }
        }
        if (key == "is_open_zhi_fu") {
          //获取当前是否支持支付宝的支付方式
          if (value == "0") {
            titles.removeAt(0);
            imageNames.removeAt(0);
          }
        }
      });
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    //获取价格单价
    String price = widget.object?["price"];

    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(topLeft: Radius.circular(16.5.w), topRight: Radius.circular(16.5.w))),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.symmetric(vertical: 10.h, horizontal: 10.w),
            child: Row(
              children: [
                Expanded(
                  flex: 1,
                  child: Container(
                    alignment: Alignment.centerLeft,
                    child: GestureDetector(
                      onTap: () {
                        //关闭
                        Navigator.pop(context);
                      },
                      child: Container(
                          padding: EdgeInsets.all(8.0.w), child: MyImage(imageName: "ic_cancel", width: 20.w)),
                    ),
                  ),
                ),
                Expanded(
                    flex: 3,
                    child: Container(
                        alignment: Alignment.center,
                        child: Text(
                          "支付: ¥$price",
                          style: TextStyle(fontSize: 17.sp, fontWeight: FontWeight.w700),
                        ))),
                const Spacer(),
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 15.w),
            child: MyGaps.line,
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 15.w),
            margin: EdgeInsets.only(top: 15.h, bottom: 10.h),
            child: Text(
              "选择支付方式",
              style: MyTextStyles.textBlackBold16,
            ),
          ),
          titles.isEmpty
              ? SizedBox(
                  height: 80.h,
                  child: const MyLoadingWidget(),
                )
              : Column(
                  children: titles.map((e) {
                    int index = titles.indexOf(e);
                    return GestureDetector(
                        onTap: () {
                          setState(() {
                            selectIndex = index;
                          });
                        },
                        child: Container(
                          color: MyColours.materialBg,
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Container(
                                    padding: const EdgeInsets.only(left: 15, right: 10),
                                    child: MyImage(imageName: imageNames[index], width: 30)),
                                Container(
                                  padding: const EdgeInsets.only(top: 17, bottom: 17, right: 10),
                                  child: Text(e, style: MyTextStyles.textSize16),
                                ),
                                const Spacer(),
                                Container(
                                    margin: const EdgeInsets.only(right: 10),
                                    padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
                                    child: MyImage(
                                        imageName: selectIndex == titles.indexOf(e) ? "ic_select" : "ic_un_select",
                                        width: 20.w)),
                              ]),
                        ));
                  }).toList(),
                ),
          GestureDetector(
            onTap: () async {
              dynamic selectCallback = widget.object!["onSelectCallback"];
              if (selectCallback != null) {
                selectCallback(imageNames[selectIndex]);
              }
              //返回上个页面
              Navigator.pop(context);
            },
            child: SafeArea(
              child: Container(
                margin: const EdgeInsets.only(top: 130, bottom: 30),
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: Container(
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  decoration: BoxDecoration(
                    color: MyColours.appMain,
                    //设置四周圆角 角度
                    borderRadius: BorderRadius.circular(25),
                  ),
                  alignment: Alignment.center,
                  child: Text("确认付款", style: MyTextStyles.textWhiteBold16),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
